package fu.capstone.hanoicityguide.presentation.fragment.holiday;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SearchView;
import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.holiday.HolidayBO;
import fu.capstone.hanoicityguide.business.bo.holiday.HolidayBOImp;
import fu.capstone.hanoicityguide.business.model.Holiday;
import fu.capstone.hanoicityguide.presentation.adapter.ListHolidayAdapter;

@SuppressLint("ValidFragment")
public class ListHolidayFragment extends Fragment implements
		SearchView.OnQueryTextListener, SearchView.OnCloseListener {

	private HolidayBO holidayBO;
	private ArrayList<Holiday> arrHoliday;
	private SearchView searchView;
	private ListHolidayAdapter holidayAdapter;
	private DrawerLayout drawer;
	private String sorter;
	private String keyword;
	private String[] sorterArr;

	public ListHolidayFragment(DrawerLayout drawer) {
		this.drawer = drawer;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View listHolidayFragment = inflater.inflate(
				R.layout.fragment_list_holiday, container, false);
		try {
			setHasOptionsMenu(true);
			ListView listHoliday = (ListView) listHolidayFragment
					.findViewById(R.id.listHoliday);

			holidayBO = new HolidayBOImp(container.getContext());
			arrHoliday = new ArrayList<Holiday>();
			setKeyword("");
			setSorter(getResources().getStringArray(R.array.sorter_items)[2]);
			sorterArr = new String[2];
			sorterArr[0] = getResources().getStringArray(R.array.sorter_items)[2];
			sorterArr[1] = getResources().getStringArray(R.array.sorter_items)[3];
			arrHoliday = holidayBO.listHoliday(getKeyword(), getSorter());
			holidayAdapter = new ListHolidayAdapter(getActivity(),
					R.layout.adapter_list_holiday, arrHoliday);
			listHoliday.setAdapter(holidayAdapter);

			listHoliday.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					Fragment browseHoliday = new DetailHolidayFragment(
							arrHoliday.get(position).getId());

					if (browseHoliday != null) {
						FragmentManager fragmentManager = getFragmentManager();
						FragmentTransaction fTransaction = fragmentManager
								.beginTransaction();
						fTransaction.addToBackStack(null);
						fTransaction.replace(R.id.frame_container,
								browseHoliday)

						.commit();
						getActivity().setTitle(
								arrHoliday.get(position).getName());
						// update selected item and title, then close the drawer
					}
				}
			});
		} catch (Exception e) {
			Log.e("ListHolidayFragment : ", e.toString());
		}

		return listHolidayFragment;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

		// create option menu in actionbar
		inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.list_holiday_menu, menu);

		// Associate searchable configuration with the SearchView
		searchView = (SearchView) menu.findItem(R.id.action_search_holiday)
				.getActionView();
		setupSearchView();
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// create event for items of option menu in actionbar
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		AlertDialog dialog;

		switch (item.getItemId()) {
		// show sorter dialog
		case R.id.action_sort_holiday:
			builder.setTitle("Sort");
			builder.setItems(sorterArr, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int item) {
					// sort by sorter
					setSorter(getResources().getStringArray(
							R.array.sorter_items)[item + 2]);

					arrHoliday = holidayBO.listHoliday(getKeyword(),
							getSorter());

					// reset array hotel in adapter
					holidayAdapter.setArr(arrHoliday);
					holidayAdapter.notifyDataSetChanged();
				}
			});

			dialog = builder.create();
			dialog.show();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume() {
		// on resume change the title of actionbar to TourProvider
		super.onResume();
		getActivity().getActionBar().setTitle("Holiday");
	}

	public String getSorter() {
		return this.sorter;
	}

	public void setSorter(String sorter) {
		this.sorter = sorter;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getKeyword() {
		return this.keyword;
	}

	private void setupSearchView() {

		searchView.setIconifiedByDefault(false);
		searchView.setQueryHint("Holiday Name");

		searchView
				.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						// TODO Auto-generated method stub
						if (hasFocus) {
							drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
						} else {
							onClose();
						}
					}
				});

		searchView.setOnQueryTextListener(this);
		searchView.setOnCloseListener(this);
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		setKeyword(query);
		if (getKeyword() != null && getSorter() != null) {
			arrHoliday = holidayBO.listHoliday(getKeyword(), getSorter());
			holidayAdapter.setArr(arrHoliday);
			holidayAdapter.notifyDataSetChanged();
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		// TODO Auto-generated method stub
		setKeyword(newText);

		if (getKeyword() != null && getSorter() != null) {
			arrHoliday = holidayBO.listHoliday(getKeyword(), getSorter());
			holidayAdapter.setArr(arrHoliday);
			holidayAdapter.notifyDataSetChanged();
			return true;
		} else {
			return false;
		}

	}

	@Override
	public boolean onClose() {
		// onclose search view
		drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
		searchView.setQuery("", true);
		setKeyword("");
		arrHoliday = holidayBO.listHoliday("", getSorter());
		holidayAdapter.setArr(arrHoliday);
		holidayAdapter.notifyDataSetChanged();
		return true;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		searchView = null;
		super.onDestroy();
	}
}
